﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DLULearningNetworkModel
{
    public partial class ScheduleTime
    {
        public static int Add(ScheduleTime _ScheduleTime)
        {
            try
            {
                using (DLULNEntities db = new DLULNEntities())
                {
                    db.ScheduleTimes.AddObject(_ScheduleTime);
                    CreateChangeScheduleFeedAndNotification(_ScheduleTime.SeminarScheduleID_u, db, "create");
                    if (db.SaveChanges() > 0)
                    {
                        return 1;
                    }
                    return 0;

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static int Update(ScheduleTime _ScheduleTime)
        {
            try
            {
                using (DLULNEntities db = new DLULNEntities())
                {
                    ScheduleTime entity = db.ScheduleTimes.SingleOrDefault(en => en.ID_u == _ScheduleTime.ID_u);
                    if (entity == null)
                    {
                        return 0;
                    }
                    entity.SeminarScheduleID_u = _ScheduleTime.SeminarScheduleID_u;
                    entity.Start_t = _ScheduleTime.Start_t;
                    entity.End_t = _ScheduleTime.End_t;
                    entity.Description_s = _ScheduleTime.Description_s;
                    CreateChangeScheduleFeedAndNotification(_ScheduleTime.SeminarScheduleID_u, db,"update");
                    if (db.SaveChanges() > 0)
                    {
                        return 1;
                    }
                    return 0;

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static int Delete(Guid _ScheduleTimeID)
        {
            try
            {
                using (DLULNEntities db = new DLULNEntities())
                {
                    ScheduleTime entity = db.ScheduleTimes.SingleOrDefault(en => en.ID_u == _ScheduleTimeID);
                    if (entity == null)
                    {
                        return 0;
                    }
                    CreateChangeScheduleFeedAndNotification(entity.SeminarScheduleID_u, db,"delete");
                    db.ScheduleTimes.DeleteObject(entity);
                    if (db.SaveChanges() > 0)
                    {
                        return 1;
                    }
                    return 0;

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //Ham nay su dung db tu ngoai truyen vao de de dang dung transaction
        static void CreateChangeScheduleFeedAndNotification(Guid seminarScheduleID, DLULNEntities db, string typeMessage)
        {
            SeminarSchedule seminarSchedule = db.SeminarSchedules.SingleOrDefault(ss => ss.SeminarScheduleID_u == seminarScheduleID);
            List<Enrollment> lstEnrollment = null;
            CourseNotification courseNotification = null;
            if (seminarScheduleID != null)
            {
                Feed newFeed = new Feed();
                newFeed.FeedID_u = Guid.NewGuid();
                newFeed.ActiveAccountID_u = seminarSchedule.Course.AccountID_u;
                newFeed.CourseID_u = seminarSchedule.Course.CourseID_u;
                switch (typeMessage)
                {
                    case "delete":
                        newFeed.Content_s = "Xóa lịch thảo luận trực tuyến ngày " + seminarSchedule.Date_dt.ToShortDateString();
                        break;
                    case "create":
                        newFeed.Content_s = "thêm lịch thảo luận trực tuyến ngày " + seminarSchedule.Date_dt.ToShortDateString();
                        break;
                    case "update":
                        newFeed.Content_s = "Thay đổi lịch thảo luận trực tuyến ngày " + seminarSchedule.Date_dt.ToShortDateString();
                        break;

                }
                newFeed.Type_s = EnumFeedType.ActivityChangeSeminarSchedule.ToString();
                newFeed.CreateDate_dt = DateTime.Now;
                newFeed.LastUpdate_dt = DateTime.Now;
                db.Feeds.AddObject(newFeed);
                lstEnrollment = seminarSchedule.Course.Enrollments.Where(en => en.Status_i == 1).ToList();
                foreach (Enrollment enrollment in lstEnrollment)
                {
                    courseNotification = new CourseNotification();
                    courseNotification.CourseNotificationID_u = Guid.NewGuid();
                    courseNotification.AccountID_u = enrollment.AccountID_u;
                    courseNotification.FeedID_u = newFeed.FeedID_u;
                    courseNotification.IsRead_b = false;
                    db.CourseNotifications.AddObject(courseNotification);
                }
            }
        }

        public static ScheduleTime SelectByID(Guid id)
        {
            using (DLULNEntities db = new DLULNEntities())
            {
                return db.ScheduleTimes.Where(st => st.ID_u == id).FirstOrDefault();
            }
        }

        public static List<ScheduleTime> SelectBySeminarScheduleID(Guid id)
        {
            DLULNEntities db = new DLULNEntities();
            return db.ScheduleTimes.Where(st => st.SeminarScheduleID_u == id).ToList();
        }
    }
}
